﻿using System.Collections.Generic;
using System.Linq;
using Apps.Web.Core;
using Apps.IBLL.LMS;
using Apps.Locale;
using System.Web.Mvc;
using Apps.Common;
using Apps.IBLL;
using Microsoft.Practices.Unity;
using Apps.Models;
using Apps.BLL.SelfRcRelated;
using Apps.Models.CustomModels;
using Apps.BLL.YANX;
using DocumentFormat.OpenXml.Office2010.Excel;
using Apps.BLL.LMS;
using DocumentFormat.OpenXml.EMMA;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.IO;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Net;
using ClosedXML.Excel;

namespace Apps.Web.Areas.LMS.Controllers
{
    public class TestDataCokeController : Controller
    {
        SelfRcRelatedBLL bll = new SelfRcRelatedBLL();
        [SupportFilter]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [SupportFilter(ActionName = "Index")]
        public JsonResult GetList()
        {
            var model = new TestDataCokeParaModel
            {
                EndDate = Utils.ObjectToStr(Request["EndDate"]),
                PageIndex = Utils.ObjToInt(Request["page"], 1),
                PageSize = Utils.ObjToInt(Request["rows"], 15),
                StartDate = Utils.ObjectToStr(Request["StartDate"]),
                TestCode = Utils.ObjectToStr(Request["TestCode"]),
                Instrument = Utils.ObjectToStr(Request["Instrument"]),
                IsExport = Utils.ObjectToStr(Request["IsExport"])
            };
            int totalCount = 0;
            List<TestDataCokeOutPutModel> list = new List<TestDataCokeOutPutModel>();
            if (model.IsExport == "exprot")
                list = bll.GetCokeList(model, ref totalCount, false);
            else
                list = bll.GetCokeList(model, ref totalCount, true);
            if (list != null)
                if (list.Count > 0)
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        list[i].CRI = Utils.ToOnePoint(list[i].CRI);
                        list[i].CSR = Utils.ToOnePoint(list[i].CSR);
                        list[i].INSTRUMENT = ReturnInstr(list[i].INSTRUMENT);
                    }
                }
            return Json(new Pagination.PaginationResult<TestDataCokeOutPutModel>(totalCount, list == null ? new List<TestDataCokeOutPutModel>() : list.ToList()));
        }

        public string ReturnInstr(string val)
        {
            if (val == "56")
                return "焦炭反应性1#";
            if (val == "57")
                return "焦炭反应性2#";
            if (val == "58")
                return "焦炭反应性3#";
            if (val == "59")
                return "焦炭反应性4#";
            if (val == "60")
                return "焦炭反应性5#";
            if (val == "61")
                return "焦炭反应性6#";
            if (val == "62")
                return "焦炭反应性7#";
            return "未知";
        }

        /// <summary>
        /// 导出 
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        [HttpGet]
        [SupportFilter(ActionName = "Index")]
        public FileResult Export(string TestCode, string Instrument, string StartDate, string EndDate)
        {
            int totalCount = 0;
            List<TestDataCokeOutPutModel> list = new List<TestDataCokeOutPutModel>();
            list = bll.GetCokeList(new TestDataCokeParaModel
            {
                TestCode = TestCode,
                Instrument = Instrument,
                StartDate = StartDate,
                EndDate = EndDate
            }, ref totalCount, false);
            if (list != null)
                if (list.Count > 0)
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        list[i].CRI = Utils.ToOnePoint(list[i].CRI);
                        list[i].CSR = Utils.ToOnePoint(list[i].CSR);
                        list[i].INSTRUMENT = ReturnInstr(list[i].INSTRUMENT);
                    }
                }
            if (list.Count > 0)
            {
                HSSFWorkbook hssfworkbook = new HSSFWorkbook();
                #region 

                IFont font = hssfworkbook.CreateFont();
                font.FontName = "宋体";
                font.FontHeightInPoints = 11;
                //表头样式
                ICellStyle style = hssfworkbook.CreateCellStyle();
                //设置单元格边框
                style.SetFont(font);

                ICellStyle styleRight = hssfworkbook.CreateCellStyle();
                styleRight.Alignment = HorizontalAlignment.Right;
                //设置单元格边框
                styleRight.SetFont(font);

                #endregion

                ISheet sheet1 = null;
                IRow rowHeader;
                int index = 0;
                int rowIndex = 0;
                sheet1 = hssfworkbook.CreateSheet($"焦炭反应性实验数据");
                rowHeader = sheet1.CreateRow(rowIndex);
                rowHeader.CreateCell(0).SetCellValue("设备编号");
                rowHeader.CreateCell(1).SetCellValue("化验码");
                rowHeader.CreateCell(2).SetCellValue("自动编号");
                rowHeader.CreateCell(3).SetCellValue("试样质量(g)");
                rowHeader.CreateCell(4).SetCellValue("试样粒数");
                rowHeader.CreateCell(5).SetCellValue("反应后残余质量(g)");
                rowHeader.CreateCell(6).SetCellValue("反应后残余粒数");
                rowHeader.CreateCell(7).SetCellValue("转鼓后大于10mm粒数级质量(g)");
                rowHeader.CreateCell(8).SetCellValue("转鼓后大于10mm粒数级粒数");
                rowHeader.CreateCell(9).SetCellValue("CRI%");
                rowHeader.CreateCell(10).SetCellValue("CSR%");
                rowHeader.CreateCell(11).SetCellValue("备注");
                rowHeader.CreateCell(12).SetCellValue("化验员");
                rowHeader.CreateCell(13).SetCellValue("化验时间");

                list.ForEach(res =>
                {
                    rowIndex++;
                    index++;

                    rowHeader = sheet1.CreateRow(rowIndex);
                    rowHeader.CreateCell(0).SetCellValue(res.INSTRUMENT);
                    rowHeader.CreateCell(1).SetCellValue(res.TEST_CODE);
                    rowHeader.CreateCell(2).SetCellValue(res.AUTO_NO);
                    rowHeader.CreateCell(3).SetCellValue(res.SAMPLE_WEIGHT);
                    rowHeader.CreateCell(4).SetCellValue(Utils.ObjToInt(res.SAMPLE_COUNT, 0));
                    rowHeader.CreateCell(5).SetCellValue(Utils.ToDouble(res.CRI_WEIGHT));
                    rowHeader.CreateCell(6).SetCellValue(Utils.ObjToInt(res.CRI_COUNT, 0));
                    rowHeader.CreateCell(7).SetCellValue(Utils.ToDouble(res.CSR_WEIGHT));
                    rowHeader.CreateCell(8).SetCellValue(Utils.ObjToInt(res.CSR_COUNT, 0));
                    rowHeader.CreateCell(9).SetCellValue(Utils.ToDouble(res.CRI));
                    rowHeader.CreateCell(10).SetCellValue(Utils.ToDouble(res.CSR));
                    rowHeader.CreateCell(11).SetCellValue(res.REMARK);
                    rowHeader.CreateCell(12).SetCellValue(res.TEST_USER);
                    rowHeader.CreateCell(13).SetCellValue(res.TEST_TIME);
                    //rowHeader.GetCell(0).CellStyle = style;
                    //rowHeader.GetCell(1).CellStyle = styleRight;
                });

                MemoryStream file = new MemoryStream();
                hssfworkbook.Write(file);
                file.Seek(0, SeekOrigin.Begin);
                byte[] excelbyte = file.ToArray();
                return File(excelbyte, @"application/ms-excel", "焦炭反应性实验数据.xls");
            }
            return null;
        }


    }
}
